package cn.zwx.activiti.example.conifig;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.sql.DataSource;

/**
 * @description: 德鲁伊监控
 * @projectName:activiti-example
 * @see:cn.zwx.activiti.example.conifig
 * @author:zhangwenxue
 * @createTime:2021/5/6 13:00
 * @version:1.0
 */
@Configuration
public class DruidConfig {


    //加载application.yaml中的Druid配置
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return  new DruidDataSource();
    }

    //配置Druid的监控
    //1、配置一个管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        // 添加IP白名单
        bean.addInitParameter("allow", "");
        // 添加IP黑名单，当白名单和黑名单重复时，黑名单优先级更高
//        bean.addInitParameter("deny", "127.0.0.1");
        // 添加控制台管理用户
        bean.addInitParameter("loginUsername", "root");
        bean.addInitParameter("loginPassword", "123456");
        // 是否能够重置数据
        bean.addInitParameter("resetEnable", "false");

        return bean;
    }

    //2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤格式
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
        return filterRegistrationBean;
    }
}
